<template>
  <div class="category-list">
    
    <div v-if="category.summary" class="category-summary mb-4">
      {{ category.summary }}
    </div>
     <div class="article-list-container" :class="{ 'has-scroll': articles.length > 5 }">
      <div class="article-list">
        <div v-for="article in articles" :key="article.articleId" class="article-item">
          <div class="article-container">
            <div v-if="article.coverImage" class="article-image">
              <router-link :to="`/article/${article.articleId}`">
                <img :src="article.coverImage" :alt="article.title" class="article-cover">
              </router-link>
            </div>
            <div class="article-content">
              <div class="article-badges">
                <span v-if="article.isTop === '1'" class="badge top-badge">置顶</span>
                <span v-if="article.isHot === '1'" class="badge hot-badge">热门</span>
                <span class="badge category-badge">{{ article.categoryName }}</span>
              </div>
              
              <h3 class="article-title">
                <router-link :to="`/article/${article.articleId}`">{{ article.title }}</router-link>
                <small v-if="article.subtitle" class="article-subtitle">{{ article.subtitle }}</small>
              </h3>
              
              <p class="article-summary">{{ article.summary }}</p>
              
              <div class="article-meta">
                <div class="meta-item">
                  <i class="el-icon-user"></i>
                  <span>{{ article.author || '未知作者' }}</span>
                </div>
                <div class="meta-item">
                  <i class="el-icon-office-building"></i>
                  <span>{{ article.source || '未知来源' }}</span>
                </div>
                <div class="meta-item">
                  <i class="el-icon-time"></i>
                  <span>{{ formatDate(article.publishTime) }}</span>
                </div>
                <div class="meta-item">
                  <i class="el-icon-view"></i>
                  <span>{{ article.viewCount || 0 }}</span>
                </div>
                <div class="meta-item">
                  <i class="el-icon-star-off"></i>
                  <span>0</span>
                </div>
                <div class="read-more">
                  <router-link :to="`/article/${article.articleId}`" class="read-more-btn">阅读全文</router-link>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="pagination-wrapper" v-if="total > pageSize">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CategoryList',
  props: {
    category: {
      type: Object,
      required: true
    },
    articles: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      total: 0,
      currentPage: 1,
      pageSize: 10
    }
  },
  methods: {
    formatDate(dateStr) {
      if (!dateStr) return '未知'
      const date = new Date(dateStr)
      return date.toLocaleDateString()
    },
    handlePageChange(page) {
      this.currentPage = page
      this.$emit('page-change', page)
    }
  },
  watch: {
    articles: {
      immediate: true,
      handler(newVal) {
        this.total = newVal.length
      }
    }
  }
}
</script>

<style scoped>
.category-container {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 120px); /* 根据实际header和footer高度调整 */
}
.category-list {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.category-summary {
  color: #666;
  line-height: 1.6;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 5px;
  margin-bottom: 30px;
}
.article-list-container {
  flex: 1;
  min-height: 400px; /* 设置最小高度 */
  display: flex;
  flex-direction: column;
}

.article-list-container.has-scroll {
  max-height: 800px;
  overflow-y: auto;
}
.article-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-item {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  overflow: hidden;
}

.article-item:hover {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.12);
  transform: translateY(-3px);
}

.article-container {
  display: flex;
}

.article-image {
  flex: 0 0 260px;
  padding: 15px;
}

.article-cover {
  width: 100%;
  height: 160px;
  object-fit: cover;
  border-radius: 4px;
  transition: transform 0.3s;
}

.article-cover:hover {
  transform: scale(1.03);
}

.article-content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.article-badges {
  margin-bottom: 10px;
  display: flex;
  gap: 8px;
}

.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.top-badge {
  background-color: #ff4d4f;
  color: white;
}

.hot-badge {
  background-color: #faad14;
  color: white;
}

.category-badge {
  background-color: #f0f0f0;
  color: #666;
}

.article-title {
  font-size: 20px;
  margin: 0 0 10px 0;
  color: #333;
}

.article-title a {
  color: inherit;
  text-decoration: none;
  transition: color 0.2s;
}

.article-title a:hover {
  color: #1890ff;
}

.article-subtitle {
  display: block;
  font-size: 14px;
  color: #888;
  margin-top: 5px;
}

.article-summary {
  color: #555;
  line-height: 1.8;
  font-size: 14px;
  margin: 0 0 15px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.article-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 20px;
  color: #888;
  font-size: 13px;
  margin-top: auto;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.meta-item i {
  font-size: 14px;
}

.read-more {
  margin-left: auto;
}

.read-more-btn {
  padding: 5px 15px;
  background: transparent;
  border: 1px solid #1890ff;
  color: #1890ff;
  border-radius: 4px;
  text-decoration: none;
  font-size: 13px;
  transition: all 0.2s;
}

.read-more-btn:hover {
  background: #1890ff;
  color: white;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 30px 0;
}

@media (max-width: 768px) {
  .article-container {
    flex-direction: column;
  }
  
  .article-image {
    flex: none;
    padding: 15px 15px 0 15px;
  }
  
  .article-cover {
    height: 180px;
  }
  
  .article-content {
    padding: 15px;
  }
  
  .article-meta {
    gap: 10px;
  }
  
  .read-more {
    margin-left: 0;
    width: 100%;
    margin-top: 10px;
  }
  
  .read-more-btn {
    display: block;
    text-align: center;
  }
}
</style>